/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.SanPham;
import util.NewHibernateUtil;

/**
 *
 * @author 0
 */
public class SanPhamDAO extends BaseDAO{
    public static List<SanPham> TimKiem(Integer maSanPham,String tenSanPham,Integer idLoaiSanPham,Double donGiaFrom,Double donGiaTo) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string="from SanPham sp where (sp.isDelete=false or sp.isDelete=null)";
        if(maSanPham!=null){
            string+=String.format(" and sp.id=%d",maSanPham);
        }
        if(!tenSanPham.isEmpty()){
            string+=" and lower(sp.tenSanPham) like :tenSanPham";
        }
        if(idLoaiSanPham!=null){
            string+=String.format(" and sp.loaiSanPham.id=%d",idLoaiSanPham);
        }
        if(donGiaFrom!=null){
            string+=String.format(" and sp.donGia>=%f", donGiaFrom);
        }
        if(donGiaTo!=null){
            string+=String.format(" and sp.donGia<=%f", donGiaTo);
        }
        Query query=session.createQuery(string);
        if(!tenSanPham.isEmpty()){
            query.setString("tenSanPham", "%"+tenSanPham.toLowerCase()+"%");
        }
        List<SanPham> sanPhams=query.list();
        session.close();
        return sanPhams;
    }
}
